﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ControlStock.Servicio;
using ControlStock.Dominio;
using System.Collections.Generic;

namespace ControlStock
{
    public partial class Articulospedidos : System.Web.UI.Page
    {
        List<Pedidos> ListaPedidos;
        PedidosServicio servicioPedidos;
        ArticuloServicio servicioArticulos;
        DataTable dt = new DataTable();
        
        public DataTable definirtabla()
        {
            
            dt.Columns.Add("Articulo_Id", typeof(string));
            dt.Columns.Add("Cantidad", typeof(Int32));
            return dt;
        }
 
             
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id_Usuario"] == null)
                Response.Redirect("index.aspx");

            if (Session["Tipo_Usuario"] == "Administrador")
                    btnprincipal.Visible = true;

            if (!Page.IsPostBack)
            {
                definirtabla();           
                //Cargo Articulos
                servicioArticulos = new ArticuloServicio();
                ddlarticulos.DataSource = servicioArticulos.Listar(Convert.ToString(Session["Tipo_Usuario"]));
                ddlarticulos.DataTextField = "Descripcion_Art";
                ddlarticulos.DataValueField = "Id_Art";
                ddlarticulos.DataBind();
               // Cargo Movimientos
                Page.Session["tabla"] = dt;
             }
        }

        protected void btnagregar_Click(object sender, EventArgs e)
        {
            btnfinalizar.Visible = true;
            DataTable NewTabla = (DataTable)Session["tabla"];
            DataRow dRow = NewTabla.NewRow();
            dRow["Articulo_Id"] = ddlarticulos.SelectedItem.Value.ToString();
            dRow["Cantidad"] = Convert.ToInt32(txtCantidad.Text);
            NewTabla.Rows.Add(dRow);
            Page.Session["tabla"] = NewTabla;
            GvPedidos.DataSource = NewTabla;
            GvPedidos.DataBind();
        }

        protected void btnfinalizar_Click(object sender, EventArgs e)
        {
            servicioPedidos = new PedidosServicio();
            int idped = servicioPedidos.Agregar(txtObservaciones.Value, Convert.ToInt32(Session["id_Usuario"]));
            DataTable dat = new DataTable();
            dat = (DataTable)Session["tabla"];
            DataRow dro = dat.NewRow();
            for (int i = 0; i < dat.Rows.Count; i++)
            {
                dro = dat.Rows[i];
                servicioPedidos.Agregarped(idped,Convert.ToInt32(dro[0]),Convert.ToInt32(dro[1]));
            }
        }

        protected void btnprincipal_Click(object sender, EventArgs e)
        {
            Response.Redirect("Principal.aspx");
        }

        protected void btneliminar_Click(object sender, EventArgs e)
        {

        }

        protected void ddlarticulos_DataBound(object sender, EventArgs e)
        {
            servicioArticulos = new ArticuloServicio();
            Articulos art =  servicioArticulos.buscar(Convert.ToInt32(ddlarticulos.SelectedValue), " ");
            if (art != null)
            {
                txtCantidad.Text = art.Cantidad_Art.ToString();
                RangeValidator1.MaximumValue = art.Cantidad_Art.ToString();
            }
            else
            {
                txtCantidad.Text = "0";
                RangeValidator1.MaximumValue = "0";
            }
        }

    }
}
